<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="com.stone.para.IConstant"%>
<%@ page import="com.stone.sys.pojo.TUser"%>
<%
	String contextPath = request.getContextPath();
	String flag = (String) request.getParameter("flag");
	
	String userid = (String) request.getParameter("userid");
	String username = (String) request.getParameter("username");
	String organid = (String) request.getParameter("organid");
	String opernerType = request.getParameter("opernerType");
	String loginUser = request.getParameter("loginUser") != null ? request.getParameter("loginUser") : ((TUser)session.getAttribute(IConstant.SESSION_TUSER)).getId().toString();

%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=IConstant.PROGRAEM_TITLE%></title>
        <link rel="stylesheet" href="<%=contextPath%>/ext3/resources/css/ext-all.css" type="text/css"   />
        <script src="<%=contextPath%>/ext3/adapter/ext/ext-base.js"></script>
        <script src="<%=contextPath%>/ext3/ext-all.js"></script>
        <script language="javascript">
        
        
String.prototype.endWith=function(str){
	if(str==null||str==""||this.length==0||str.length>this.length)
	return false;
	if(this.substring(this.length-str.length)==str)
	return true;
	else
	return false;
	return true;
	}
	
String.prototype.trim= function() 
{ 
return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
        
        
            Ext.BLANK_IMAGE_URL = '<%=contextPath%>/ext3/resources/images/default/s.gif';
            Ext.QuickTips.init();
            Ext.form.Field.prototype.msgTarget = 'qtip';
            var cpath = '<%=contextPath%>';
		    var checking = false;
		    var tree;
		    var oOption;
		    
		    
		   
			Ext.onReady(function(){
			
			var tb = new Ext.Toolbar({
		    renderTo: document.all.tbar,
		    items: [
 				'->',{
			        text: '确定',
			        icon:"<%=contextPath%>/images/pass.gif",
			        handler:setUser
			    }/*,'-',
			    {
			        text: '清空',
			        icon:"<%=contextPath%>/images/clear.png",
			        handler:function() {
			         	// 用户ID
						window.opener.document.frmM.elements["<%=userid%>"].value = "";

						// 用户名
						window.opener.document.frmM.elements["<%=username%>"].value = "";
			        }
			    }*/
		    ]
			});
	
			var storeSQ = new Ext.data.Store({
        		proxy:new Ext.data.HttpProxy({url:'userList!getListExtTree.do?state=user'}),
        		reader:new Ext.data.JsonReader({},['id','text','qtip','parentId']),
        		autoLoad:true,
        		listeners:{"load":function(store,records){
 
        			if (records != null && records != "" && records.length > 0) {
        							
        				var baseLength = (records[0].data.qtip + "").length;
        				
        				// 已选择用户
        				var arrayoldid = window.opener.document.frmM.elements["<%=userid%>"].value.trim().split(",");
        				
						for (var i=0; i<records.length; i++){
						
	       					var n = new Ext.tree.TreeNode({
                								id:records[i].data.id,
                								text:records[i].data.text,
                								qtip:records[i].data.qtip,
                								leaf:true,
                								checked:false
							});
											
							// 第一层菜单时
							if (i == 0) { // || (records[i].data.qtip + "").length == baseLength) {
								
								tree.root.appendChild(n);
								// 下级菜单
							} else {
							
								try {
									if (n.attributes.qtip =="000") {
										n.attributes.icon = cpath + '/images/user_suit.png';
									}
    								tree.getNodeById(records[i].data.parentId).leaf = false;
    								tree.getNodeById(records[i].data.parentId).appendChild(n);
    											
    							} catch (e) {
    							}
								
							}
   						}  // for (var i=0; i<records.length; i++){
			
					checking = true;			
									
					// 选中已有的人员
					for (var j=0; j<arrayoldid.length; j++) {
						if (arrayoldid[j] != "") {
							tree.getNodeById(arrayoldid[j]).attributes.checked = true;
							checkParent(tree.getNodeById(arrayoldid[j]));
							
						}

					}
			
					checking = false;
					//tree.expandAll();
					
					if (arrayoldid.length ==0 || (arrayoldid.length ==1 && arrayoldid[0]=="")) {
				        tree.root.expandChildNodes();
						// 变电工区展开
						tree.root.firstChild.expandChildNodes();//    tree.getNodeById("A1").expandChildNodes();
						//for(var i =0;i<tree.root.firstChild.childNodes.length;i++) {
						//	if(tree.root.firstChild.childNodes[i].id  != tree.getNodeById("<%=loginUser%>").parentNode.id) {
						//		tree.root.firstChild.childNodes[i].collapse();
						//	}
						//}
						tree.getNodeById("<%=loginUser%>").parentNode.expand(true);
				        tree.getNodeById("<%=loginUser%>").parentNode.expandChildNodes();
					} else {

						// root 展开
						tree.root.expandChildNodes();
						// 变电工区展开
						tree.root.firstChild.expandChildNodes();//    tree.getNodeById("A1").expandChildNodes();
						//for(var i =0;i<tree.root.firstChild.childNodes.length;i++) {
						//	if(tree.root.firstChild.childNodes[i].id  != tree.getNodeById(arrayoldid[0]).parentNode.id) {
						//		tree.root.firstChild.childNodes[i].collapse(true);
						//	}
						//}
						//window.alert(arrayoldid.length);window.alert(arrayoldid[0]);
						tree.getNodeById(arrayoldid[0]).parentNode.expand(true);
						tree.getNodeById(arrayoldid[0]).parentNode.expandChildNodes(true);
					}
					
				}   // if (records != null && records != "" && records.length > 0) {
            }} // 
                    			 
    	});

            
		/* 生成Ttee,放到tree Div里面 */
 		tree = new Ext.tree.TreePanel({
                        id:"mytree",   
                        renderTo : "tree",
                        rootVisible:false,
                        root : new Ext.tree.TreeNode({   
                    			id : "root", 
                    			draggable:false, 
                    			singleClickExpand:true, 
                    			text : "<%=IConstant.PROGRAEM_TITLE%>"
	 						}),  
                        store : storeSQ,   
                        width : 300,   
                        height : 480,
				        useArrows:true,
				        autoScroll:true,
				        animate:true,
				        containerScroll:true,
				        checked:false,
				        listeners:{"click":function(node){ 
												
														// 参照
														try {
															if(node.attributes.qtip != '000') {
																Ext.MessageBox.alert('提示', '请选择人员');
																return;
															}
															window.opener.document.frmM.elements["<%=userid%>"].value = node.id;
															window.opener.document.frmM.elements["<%=username%>"].value = node.text;
															
														} catch (e) {
														
														}
														window.close();	
													
                    	}}
				});
				
		// checkbox触发事件
		tree.on('checkchange', function(node,checked) { 
	
			if(checking){ 
				return false;
			}
				
			checking = true;
			// 选中的话
			if(node.ui.isChecked()){
				checkChildren(node,true);
				checkParent(node);
			if(<%=opernerType%>=='2'){//window.alert(node.id);
		    window.opener.gzapCyCheck(node.id,node.text);
			}
			// 没选中
			} else {
				checkChildren(node,false);
				uncheckParent(node);
			}
		
			checking = false;
		});
				
				
		// 子结点处理
		function checkChildren(node, checked)
		{
			//for(var i =0;i<node.childNodes.length;i++) {
			//	node.childNodes[i].ui.toggleCheck(checked);
			//	checkChildren(node.childNodes[i], checked);
			//}
	       node.attributes.checked = checked;
	       node.ui.toggleCheck(checked);
	       node.eachChild(function(child){
	         checkChildren(child,checked);
	       }); 
		}
	
		// 选中所有父结点
		function checkParent(node) {
	
			if(node.parentNode == null){
				return false;
			}
			node.parentNode.attributes.checked = true;
			node.parentNode.ui.toggleCheck(true);
			checkParent(node.parentNode);
		}
	
		// 取消所有父结点
		function uncheckParent(node) {
			if(node.parentNode == null) {
				return false;
			}
	
			var blCheck = true;
			for(var i =0;i<node.parentNode.childNodes.length;i++) {
				if(node.parentNode.childNodes[i].ui.isChecked()) {
					blCheck = false;
				}
			}
			if(blCheck) {
				node.parentNode.ui.toggleCheck(false);
				uncheckParent(node.parentNode);
			}
		}
	 
	 	/* 
	 	function toExpandChildNodes(node) {
	 	
			if(node.parentNode == null){
				return false;
			} else {
			alert();
				node.parentNode.expandChildNodes();
			}
			toExpandChildNodes(node.parentNode);
	 	}*/
 });
 		
 		function setUser() {

 			// 用户ID
			window.opener.document.frmM.elements["<%=userid%>"].value = "";

			// 用户名
			window.opener.document.frmM.elements["<%=username%>"].value = "";
 		    
 			getCheckMenuID(tree.root.firstChild);
 			
 			var userid = window.opener.document.frmM.elements["<%=userid%>"].value;
 			var username = window.opener.document.frmM.elements["<%=username%>"].value
 			
 			if (userid.endWith(",")) {
 				window.opener.document.frmM.elements["<%=userid%>"].value = 
 					userid.substring(0,userid.length-1);
 			}
 			
 			if (username.endWith(",")) {
 				window.opener.document.frmM.elements["<%=username%>"].value = 
 					username.substring(0,username.length-1);
 			}
 			
 			window.close();
 		}
 		
	// 过滤选择选中的复选框
	function getCheckMenuID(node) {

		if (node.ui.isChecked()) {
	
			if (node.attributes.qtip =="000") {
			
				// 用户ID
				window.opener.document.frmM.elements["<%=userid%>"].value = 
					window.opener.document.frmM.elements["<%=userid%>"].value + node.id + ",";

				// 用户名
				window.opener.document.frmM.elements["<%=username%>"].value = 
					window.opener.document.frmM.elements["<%=username%>"].value + node.text + ",";
			
			}
    		for (var i=0;i<node.childNodes.length;i++) {
    			getCheckMenuID(node.childNodes[i]);
    		}
		}
	}

		</script>
	</head>
	<body>
		<div id = "tbar" style="position:absolute;display:block left:0px; top:0px; width:100%; height:35px; z-index:1;"></div>
		<br>
		<div id="tree" style="position:absolute;display:block left:0px; top:35px; width:100%; height:95%; z-index:1000; overflow:auto;">
		</div>
	</body>
</html>
